/**
 *    Copyright (C) 2025-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */
#pragma once

#include "mongo/db/query/compiler/optimizer/join/join_graph.h"
#include "mongo/db/query/compiler/optimizer/join/plan_enumerator.h"
#include "mongo/db/query/compiler/optimizer/join/solution_storage.h"
#include "mongo/db/query/multiple_collection_accessor.h"
#include "mongo/util/modules.h"

namespace mongo::join_ordering {

struct ReorderedJoinSolution {
    std::unique_ptr<QuerySolution> soln;
    // Base node of new join order.
    NodeId baseNode;
};

/**
 * Represent sargable predicate that can be the RHS of an indexed nested loop join.
 */
struct IndexedJoinPredicate {
    QSNJoinPredicate::ComparisonOp op;
    FieldPath field;
};

/**
 * Construct a 'QuerySolution' with a pseudo-random join ordering based on the given seed. Performs
 * a random walk of the join graph to determine the join order and uses the given 'QuerySolutionMap'
 * to lookup the corresponding single-table access plan for each node. 'defaultHJ' specifies if a
 * hash join should be the default instead of a nested loop join if an INLJ is not possible.
 */
ReorderedJoinSolution constructSolutionWithRandomOrder(
    QuerySolutionMap solns,
    const join_ordering::JoinGraph& joinGraph,
    const std::vector<ResolvedPath>& resolvedPaths,
    const MultipleCollectionAccessor& catalog,
    int seed,
    bool defaultHJ = false);

/**
 * Construct a 'QuerySolution' with a join ordering based on the result generated by bottom-up
 * Sellinger-style join optimization.
 */
ReorderedJoinSolution constructSolutionBottomUp(QuerySolutionMap solns,
                                                const join_ordering::JoinGraph& joinGraph,
                                                const std::vector<ResolvedPath>& resolvedPaths,
                                                const MultipleCollectionAccessor& catalog,
                                                PlanTreeShape shape);

/**
 * Returns true if the given join predicates can be satisfied by an "index probe" (RHS of INLJ) on
 * the given index.
 */
bool indexSatisfiesJoinPredicates(const IndexCatalogEntry& ice,
                                  const std::vector<IndexedJoinPredicate>& joinPreds);

/**
 * Returns the best index that can satify the indexed predicates by "index probe". If there is no
 * index that can satisfy it then return boost::none. If multiple indexes can satisfy the join
 * predicates, this function selects one using deterministic heuristics:
 *  1. Prefer the index with fewer fields.
 *  2. If tied, prefer the index whose key pattern is lexicographically earlier.
 */
boost::optional<IndexEntry> bestIndexSatisfyingJoinPredicates(
    const IndexCatalog& indexCatalog, const std::vector<IndexedJoinPredicate>& joinPreds);

}  // namespace mongo::join_ordering
